﻿using SimpleIpc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleIpcDemo
{
    public interface IRemoteObject
    {
        int MathAdd(int a, int b);
    }
    public class RemoteObject : MarshalByRefObject, IRemoteObject
    {
        public int MathAdd(int a, int b) => a + b;
    }

    class Program
    {
        static void Main(string[] args)
        {
            IRemoteChannel ipcServer = RemoteChannelFactory.CreateIpcChannel("ipcServer", "ipcServer");
            ipcServer.RegisterChannel();
            ipcServer.RegisterWellKnownService<RemoteObject>("remoteObj");

            IRemoteChannel ipcClient = RemoteChannelFactory.CreateIpcChannel("ipcClient", "");
            ipcClient.RegisterChannel();
            var remoteObj = ipcClient.GetObject<IRemoteObject>("ipc://ipcServer/remoteObj");

            IRemoteChannel tcpServer = RemoteChannelFactory.CreateTcpChannel("tcpServer", 9900);
            tcpServer.RegisterChannel();
            tcpServer.RegisterWellKnownService<RemoteObject>("remoteObj");

            IRemoteChannel tcpClient = RemoteChannelFactory.CreateTcpChannel("tcpClient");
            tcpClient.RegisterChannel();
            var remoteObjTcp = tcpClient.GetObject<IRemoteObject>("tcp://localhost:9900/remoteObj");

            Console.WriteLine("remoteObj add: {0}", remoteObj.MathAdd(10, 12));

            Console.WriteLine("remoteObj add(tcp): {0}", remoteObj.MathAdd(112, 12));

            var list = RemoteChannelFactory.GetChannels();

            ipcServer.UnregisterChannel();
            ipcClient.UnregisterChannel();
            tcpServer.UnregisterChannel();
            tcpClient.UnregisterChannel();

            list = RemoteChannelFactory.GetChannels();

            Console.ReadLine();
        }
    }
}
